﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ThridGroup.ERP.DTO;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.ObjectMapping;

namespace ThridGroup.ERP.ProductionSystem
{
    public class ItemSettingsServices : ApplicationService, IItemSettingsServices
    {
        /// <summary>
        /// 检测项设置接口
        /// </summary>
        private readonly IRepository<ItemSettings,int> _repositories;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="repositories">检测项设置</param>
        public ItemSettingsServices(IRepository<ItemSettings, int> repositories)
        {
            _repositories = repositories;
        }
        /// <summary>
        /// 添加检测项设置
        /// </summary>
        /// <param name="itemSettings">检测项设置</param>
        /// <returns></returns>
        public async Task<int> AddItemSettings(ItemSettingsDTO itemSettings)
        {
            var entity = ObjectMapper.Map<ItemSettingsDTO, ItemSettings>(itemSettings);
            var result= await _repositories.InsertAsync(entity);
            return result == null ? 0 : 1;
        }
        /// <summary>
        /// 删除检测项设置
        /// </summary>
        /// <param name="id">要删除的id</param>
        /// <returns></returns>
        public async Task<int> DelItemSettings(int id)
        {
           var result= _repositories.DeleteAsync(m=>m.Id==id);
            return result == null ? 0 : 1;
        }
        /// <summary>
        /// 批量删除检测项设置 
        /// </summary>
        /// <param name="ids">要批量删除的id</param>
        /// <returns></returns>
        public async Task DelMany(List<int> ids)
        {
            await _repositories.DeleteDirectAsync(m => ids.Contains(m.Id));
        }
        /// <summary>
        /// 获取检测项设置
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ItemSettingsDTO> GetItemSettings(int id)
        {
            var entity = await _repositories.FindAsync(m => m.Id == id);
            var result = ObjectMapper.Map<ItemSettings, ItemSettingsDTO>(entity);
            return result;
        }
        /// <summary>
        /// 检测项设置列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="itemSettingsName"></param>
        /// <returns></returns>
        public async Task<List<ItemSettingsDTO>> ShowItemSettings(int pageIndex = 1, int pageSize = 10, string? itemSettingsName = "")
        {
            var entity = await _repositories.GetListAsync();
            entity = entity.WhereIf(!string.IsNullOrEmpty(itemSettingsName), m => m.ItemSettingsName.Contains(itemSettingsName)).ToList();
            var result = ObjectMapper.Map<List<ItemSettings>, List<ItemSettingsDTO>>(entity);
            return result;
        }
        /// <summary>
        /// 编辑检测项设置
        /// </summary>
        /// <param name="itemSettings"></param>
        /// <returns></returns>
        public async Task<int> UpdateItemSettings(ItemSettingsDTO itemSettings)
        {
            var entity = ObjectMapper.Map<ItemSettingsDTO, ItemSettings>(itemSettings);
            var result = await _repositories.UpdateAsync(entity);
            return result == null ? 0 : 1;
        }
    }
}
